package com._520it.crm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com._520it.crm.service.ICoefficientService;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
public class CoefficientController {
	@Autowired
	private ICoefficientService service;

	@RequestMapping("/coefficient/index")
	public String index() {
		return "coefficient/index";
	}

	@RequestMapping("/coefficient/list/view")
	public ModelAndView selectAll(ModelAndView modelAndView,
								  @RequestParam(defaultValue = "1") int current,
								  @RequestParam(defaultValue = "10") int size,
								  String exemptFloat,
								  String roadAccident,
								  String unlawfulAct,
								  Integer drinkAct,
								  Integer ebrietyAct,
								  @DateTimeFormat(pattern="yyyy-MM-dd") Date begin
	) {
		modelAndView.setViewName("coefficient/list");
		modelAndView.addObject("current",current);
		modelAndView.addObject("totalCount",service.selectAll().size()/size);
		modelAndView.addObject("list",service.selectPage(current,size,exemptFloat,roadAccident,unlawfulAct,
				drinkAct,ebrietyAct));
		int count = service.selectPageCount(exemptFloat,roadAccident,unlawfulAct,
				drinkAct,ebrietyAct);
		int totalCount = count%size==0?count/size:count/size+1;
		modelAndView.addObject("totalCount",totalCount);
		modelAndView.addObject("current",current);
		modelAndView.addObject("date",begin);
		return modelAndView;
	}
}